package rush.rushcampus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import rush.rushcampus.common.BaseContext;
import rush.rushcampus.dto.ErrandDto;
import rush.rushcampus.dto.ErrandOrtherDto;
import rush.rushcampus.dto.ErrandPageDto;
import rush.rushcampus.dto.ErrandReceiveDto;
import rush.rushcampus.entity.Errand;
import rush.rushcampus.entity.ErrandMail;
import rush.rushcampus.entity.ErrandOrther;
import rush.rushcampus.entity.ErrandReceive;
import rush.rushcampus.mapper.ErrandMailMapper;
import rush.rushcampus.mapper.ErrandMapper;
import rush.rushcampus.mapper.ErrandOrtherMapper;
import rush.rushcampus.mapper.ErrandReceiveMapper;
import rush.rushcampus.service.IErrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import rush.rushcampus.vo.ErrandVo;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rush
 * @since 2024-09-06
 */
@Service
public class ErrandServiceImpl extends ServiceImpl<ErrandMapper, Errand> implements IErrandService {
    @Autowired
    private ErrandMailMapper errandMailMapper;
    @Autowired
    private ErrandReceiveMapper errandReceiveMapper;
    @Autowired
    private ErrandOrtherMapper errandOrtherMapper;
    @Autowired
    private ErrandMapper errandMapper;

    @Transactional
    @Override
    public void add(ErrandDto errandDto) {
        Errand errand = new Errand();
        //1.添加子表数据
        if (errandDto.getErrandMailDto() != null){
            ErrandMail errandMail = new ErrandMail();
            BeanUtils.copyProperties(errandDto.getErrandMailDto(),errandMail);
            errandMailMapper.insert(errandMail);
            errand.setMailId(errandMail.getId());
        }
        if (errandDto.getErrandReceiveDto() != null){
            ErrandReceive errandReceive = new ErrandReceive();
            BeanUtils.copyProperties(errandDto.getErrandReceiveDto(),errandReceive);
            errandReceiveMapper.insert(errandReceive);
            errand.setRecreciveId(errandReceive.getId());
        }
        if (errandDto.getErrandOrtherDto() != null){
            ErrandOrther errandOrther = new ErrandOrther();
            BeanUtils.copyProperties(errandDto.getErrandOrtherDto(),errandOrther);
            errandOrtherMapper.insert(errandOrther);
            errand.setOthersId(errandOrther.getId());
        }
        //2.添加主表数据
        errand.setTotalFee(errandDto.getTotalFee());
        errand.setTypeId(errandDto.getTypeId());
        errand.setTypeTitle(errandDto.getTypeTitle());
        errand.setStatus(0);
        errand.setIsPay(0);
        errand.setCreateTime(LocalDateTime.now());
        errand.setUserId(BaseContext.getCurrenId());
        this.save(errand);
    }
    @Transactional
    @Override
    public void removeErrand(Long id) {
        //1.删除子表数据
        Errand errand = this.getById(id);
        if (errand.getMailId() != null){
            errandMailMapper.deleteById(errand.getMailId());
        }
        if (errand.getRecreciveId() != null){
            errandReceiveMapper.deleteById(errand.getRecreciveId());
        }
        if (errand.getOthersId() != null){
            errandOrtherMapper.deleteById(errand.getOthersId());
        }
        //2.删除主表数据
        this.removeById(id);
    }

    @Override
    public List<ErrandVo> listByUser(ErrandPageDto errandPageDto) {
        Long UserId = 1L;
        return ErrrandVoList(errandPageDto, UserId);
    }

    /**
     *
     * @param errandPageDto
     * @param UserId 主要区分是不是null
     * @return
     */
    public List<ErrandVo> ErrrandVoList(ErrandPageDto errandPageDto , Long UserId) {
        Integer currentPage = errandPageDto.getPage();
        Integer pageSize = errandPageDto.getSize();
        List<ErrandVo> errandVoList = new ArrayList<>();
        Page<Errand> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Errand> queryWrapper = new QueryWrapper<>();
        //区分有无userid决定ErrrandVoList的用途
        if (UserId != null){
            if (errandPageDto.getClassify() == 0){ ///这里的Classify是分类发布和接受的
                queryWrapper.eq("user_id", 1L);
            }else {
                queryWrapper.eq("accept_user", 1L);
            }
            queryWrapper.eq("type_id", errandPageDto.getTypeId());
        }else {
            queryWrapper.eq("status", 0);
        }
        queryWrapper.eq(errandPageDto.getTypeId()!= null, "type_id", errandPageDto.getTypeId());
        //当typeId为0时，获取全部
        queryWrapper.eq(errandPageDto.getClassify()!= null && errandPageDto.getClassify() != 0, "accept_user", 1L);
        queryWrapper.orderByDesc("create_time");
        List<Errand> errandList = errandMapper.selectPage(page, queryWrapper).getRecords();
        for (Errand errand : errandList) {
            ErrandVo errandVo = new ErrandVo();
            errandVo.setTotalFee(errand.getTotalFee());
            errandVo.setTypeId(errand.getTypeId());
            errandVo.setTypeTitle(errand.getTypeTitle());
            errandVo.setStatus(errand.getStatus());
            errandVo.setIsPay(errand.getIsPay());
            errandVo.setCreateTime(errand.getCreateTime());
            //查other表
            ErrandOrther errandOrther1 = errandOrtherMapper.selectById(errand.getOthersId());
            ErrandOrtherDto errandOrtherDto = new ErrandOrtherDto();
            BeanUtils.copyProperties(errandOrther1, errandOrtherDto);
            errandVo.setErrandOrtherDto(errandOrtherDto);

            //查receive表
            ErrandReceive errandReceive1 = errandReceiveMapper.selectById(errand.getRecreciveId());
            ErrandReceiveDto errandReceiveDto = new ErrandReceiveDto();
            BeanUtils.copyProperties(errandReceive1, errandReceiveDto);
            errandVo.setErrandReceiveDto(errandReceiveDto);

            errandVoList.add(errandVo);
        }
        return errandVoList;
    }

    @Override
    public void accept(Long id) {
        //1.修改跑腿状态
        Errand errand = this.getById(id);
        errand.setStatus(1);
        // 2.修改跑腿接受人
        errand.setAcceptUser(1L);
        // 3.修改跑腿接受时间
        errand.setAcceptTime(LocalDateTime.now());
        //4.修改预计完成时间（当前时间8小时后）
        errand.setForecastTime(LocalDateTime.now().plusHours(8));
        this.updateById(errand);
    }

    @Override
    public void finish(Long id) {
        /// 每次有新的状态时，要把旧的状态更新过来
        //1.查看是否逾期
        //2.检查订单状态
        //3.修改跑腿状态

    }
}
